using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Portal.Modules.AdminPortal;
using System.IO;
using Portal.API.Statistics.Service;
using System.Collections.Generic;

namespace Portal
{
	/// <summary>
	/// Startup Page.
	/// </summary>
	public partial class StartPage : System.Web.UI.Page
	{
		protected void Page_Load(object sender, System.EventArgs e)
		{
			cssLink.Href = Portal.Helper.CssPath;

			Control tabContent = null;
			// Depending on current mode, load the Content Control.
			if (Request["Edit"] == "Tab")
			{
				// Try to load the Tab in Edit Mode.
				Portal.PortalDefinition.Tab editTab = Helper.GetEditTab(Request["tabRef"]);
				if (editTab != null)
				{
					Tab tabControl = (Tab)LoadControl("Modules/AdminPortal/Tab.ascx");
					tabControl.LoadData(editTab);
					tabContent = tabControl;
				}
				else
				{
					tabContent = null;
				}
			}

			if (tabContent == null)
			{
				tabContent = LoadControl("PortalTab.ascx");
				if (!IsPostBack)
				{
					RequestStatisticService service = (RequestStatisticService)Portal.API.Statistics.Statistic.GetService(typeof(RequestStatisticService));
					if (null != service) service.AddRequest(Context);
				}
			}
			tabContent.ID = "content";
			contentPlace.Controls.Add(tabContent);

			try
			{
				string title = System.Configuration.ConfigurationManager.AppSettings["Title"];
				if (!string.IsNullOrEmpty(title))
					this.Title = HttpUtility.HtmlEncode(title);
				else
					this.Title = "Personal .NET Portal";
			}
			catch (System.Configuration.ConfigurationErrorsException) { }
			catch (ArgumentNullException) { }
			catch (FormatException) { }

			// Set the portal footer visible depending on the web config settings.
			try
			{
				footer.Visible = Boolean.Parse(System.Configuration.ConfigurationManager.AppSettings["UsePortalFooter"]);
			}
			catch (System.Configuration.ConfigurationErrorsException) { }
			catch (ArgumentNullException) { }
			catch (FormatException) { }

			// Set the favicon visible if the file exists.
			try
			{
				string faviconPath = System.Configuration.ConfigurationManager.AppSettings["FavIconPath"];
				favicon.Href = faviconPath;
				favicon.Visible = !string.IsNullOrEmpty(faviconPath);
			}
			catch (System.Configuration.ConfigurationErrorsException) { }

			try
			{
				string description = System.Configuration.ConfigurationManager.AppSettings["Description"];
				metaDescription.Content = description;
				metaDescription.Visible = !string.IsNullOrEmpty(description);
			}
			catch (System.Configuration.ConfigurationErrorsException) { }

			try
			{
				string author = System.Configuration.ConfigurationManager.AppSettings["Author"];
				metaAuthor.Content = author;
				metaAuthor.Visible = !string.IsNullOrEmpty(author);
			}
			catch (System.Configuration.ConfigurationErrorsException) { }

			try
			{
				string keywords = System.Configuration.ConfigurationManager.AppSettings["Keywords"];
				metaKeywords.Content = keywords;
				metaKeywords.Visible = !string.IsNullOrEmpty(keywords);
			}
			catch (System.Configuration.ConfigurationErrorsException) { }

			try
			{
				string robots = System.Configuration.ConfigurationManager.AppSettings["Robots"];
				metaRobots.Content = robots;
				metaRobots.Visible = !string.IsNullOrEmpty(robots);
			}
			catch (System.Configuration.ConfigurationErrorsException) { }

			// Disable browser cache if the user is authenticated.
			if (User.Identity.IsAuthenticated)
			{
				bool preventBrowserCache = false;
				bool.TryParse(System.Configuration.ConfigurationManager.AppSettings["PreventBrowserCache"], out preventBrowserCache);
				if (preventBrowserCache)
					Response.Cache.SetCacheability(HttpCacheability.ServerAndNoCache);
			}
			phHeader.Controls.Add(LoadControlPara("PortalHeader.ascx", "PortalHeader.htm"));

		}

		private UserControl LoadControlPara(string UserControlPath, params object[] constructorParameters)
		{
			List<Type> constParamTypes = new List<Type>();
			foreach (object constParam in constructorParameters)
			{
				constParamTypes.Add(constParam.GetType());
			}

			UserControl ctl = Page.LoadControl(UserControlPath) as UserControl;

			// Find the relevant constructor
			System.Reflection.ConstructorInfo constructor = ctl.GetType().BaseType.GetConstructor(constParamTypes.ToArray());

			//And then call the relevant constructor
			if (constructor == null)
			{
				throw new MemberAccessException("The requested constructor was not found on : " + ctl.GetType().BaseType.ToString());
			}
			else
			{
				constructor.Invoke(ctl, constructorParameters);
			}

			// Finally return the fully initialized UC
			return ctl;
		}


		override protected void OnInit(EventArgs e)
		{
			InitializeComponent();
			base.OnInit(e);
		}

		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
		}
	}
}